package com.wnxy.sddfp.bank.controller;


import com.wnxy.sddfp.bank.mq.MessageProducer;
import com.wnxy.sddfp.bank.service.ITradeLogService;
import com.wnxy.sddfp.common.enums.BorrowRecordStatusDTO;
import com.wnxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.wnxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDTO;
import com.wnxy.sddfp.common.model.dto.bid.BankBidDTO;
import com.wnxy.sddfp.common.model.dto.bid.BidBalanceDTO;
import com.wnxy.sddfp.common.model.vo.bid.LoanVo;
import com.wnxy.sddfp.common.model.vo.trade.BorrowRecordVo;
import com.wnxy.sddfp.common.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2023-04-27
 */
@RestController
@Slf4j
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;
    @Autowired
    private MessageProducer messageProducer;
    // 测试Ribbon负载均衡
    @Value("${server.port}")
    private String port;

    /**
     * 请求方式：post
     * 请求参数：
     *      reqCode  请求流水号
     *      userId   用户id
     *  返回数据：
     *      SignatureDTO
     *  访问地址：http://localhost:5500/bank/access/card
     */
    @ApiOperation("绑卡生成交易记录")
    @PostMapping("/bank/access/card")
    public Result proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        log.info("port:{}",port);
        SignatureDTO signatureVo = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return Result.ok(signatureVo);
    }

    /**
     * 银行托管系统回调的接口；根据流水号更新交易记录的表的状态
     * @param bankCardRequestDto
     */
    @PostMapping("/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto){
        log.info("绑卡回调：{}",bankCardRequestDto);
        //1、根据请求流水号修改交易记录的状态
        tradeLogService.modifyStatus(bankCardRequestDto.getReqCode());
        //2、发送消息，异步通知后台管理系统绑卡成功
        messageProducer.sendMsg(bankCardRequestDto);
        return "OK";
    }

    /**
     * 借款审核，同步借款数据
     */
    @ApiOperation("借款审核，同步借款数据")
    @PostMapping("/bank/access/sync/borrow")
    public Result syncBorrowRecord(@RequestBody BorrowRecordVo borrowRecordVo) {
        tradeLogService.syncBorrowRecord(borrowRecordVo);
        return Result.ok();
    }

    /**
     * 获取机构账户余额
     */
    @ApiOperation("获取机构账户余额")
    @PostMapping("/bank/access/bid/balance/{userCode}")
    public Result<BidBalanceDTO> getBidBalance(@PathVariable("userCode") String userCode) {
        BidBalanceDTO bidBalanceDTO = tradeLogService.getBidBalance(userCode);
        return Result.ok(bidBalanceDTO);
    }

    /**
     * 访问银行托管同步机构投资申购数据
     */
    @ApiOperation("机构投资申购数据")
    @PostMapping("/bank/access/bid/add")
    public Result addBankBidAgency(@RequestBody BankBidDTO bankBidDTO){
        tradeLogService.addBankBidAgency(bankBidDTO);
        return Result.ok();
    }

    /**
     * 放款确认:访问银行托管同步机构投资申购数据、扣除投资人冻结金额、添加到借款人账户中
     */
    @ApiOperation("放款确认")
    @PostMapping("/bank/access/confirm-loan")
    public Result confirmLoan(@RequestBody LoanVo loanVo){
        tradeLogService.confirmLoan(loanVo);
        return Result.ok();
    }

    /**
     * 同步借款状态
     */
    @ApiOperation("同步借款状态")
    @PostMapping("/bank/access/borrowRecord/status")
    public Result<String> updateBorrowRecordStatus(@RequestBody BorrowRecordStatusDTO brsDTO){
        tradeLogService.updateBorrowRecordStatus(brsDTO);
        return Result.ok();
    }


}

